plugins {
    id "com.github.node-gradle.node"
}

if (project.hasProperty("nodeInstall")) {
    node {
        version = "<%= nodeVersion %>"
        npmVersion = "<%= nodeDependencies.npm %>"
        download = true
    }

    // Copy local node and npm to a fixed location for npmw
    def deleteOldNpm = tasks.register("deleteOldNpm", Delete) {
        delete('<%= temporaryDir %>node/lib/node_modules/npm')
    }
    def fixedNode = tasks.register("fixedNode", Copy) {
        from(nodeSetup)
        into('<%= temporaryDir %>node')
        finalizedBy(deleteOldNpm)
    }
    tasks.named("nodeSetup").configure { finalizedBy(fixedNode) }

    def fixedNpm = tasks.register("fixedNpm", Copy) {
        from(npmSetup)
        into('<%= temporaryDir %>node')
    }
    tasks.named("npmSetup").configure { finalizedBy(fixedNpm) }
}

<%_ if (!skipClient) { _%>
task webapp_test(type: NpmTask) {
    inputs.property('appVersion', project.version)
    inputs.files("build.gradle")
        .withPropertyName('build.gradle')
        .withPathSensitivity(PathSensitivity.RELATIVE)
    inputs.files(<%- javaNodeBuildPaths.filter(file => !file.endsWith('/')).map(file => `'${file}'`).join(', ') %>)
        .withPropertyName('<%= clientFramework %>-build')
        .withPathSensitivity(PathSensitivity.RELATIVE)
  <%_ for (const file of javaNodeBuildPaths.filter(file => file.endsWith('/'))) { _%>
    inputs.dir("<%= file %>")
        .withPropertyName("<%= file %>")
        .withPathSensitivity(PathSensitivity.RELATIVE)
  <%_ } _%>

    outputs.dir("build/test-results/jest/")
        .withPropertyName("jest-result-dir")
    outputs.file("build/test-results/TESTS-results-jest.xml")
        .withPropertyName("jest-result")
        outputs.file("build/test-results/clover.xml")
        .withPropertyName("clover-result")

    dependsOn(npmInstall, compileTestJava)
    args = ["run", "webapp:test"]
}

test.dependsOn(webapp_test)

<%_ } _%>
